package com.xunsiya.service.system;

import com.xunsiya.system.dao.AuthMapper;
import com.xunsiya.system.dao.GroupMapper;
import com.xunsiya.system.dao.UserMapper;
import com.xunsiya.system.vo.GroupInfo;
import com.xunsiya.system.vo.User;
import com.xunsiya.tools.common.msg.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 分组实现层
 *
 * @author linqy
 * @version 1.0
 *          <p/>
 *          <br/>
 *          <br/>修订人    修订时间      描述信息
 *          <br/>-----------------------------------------------------
 *          <br/>linqy    2017/11/11    初始创建
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class GroupService {

	@Autowired
	private GroupMapper groupMapper;
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private AuthMapper authMapper;
	
	public Msg saveGroup(GroupInfo group) {
		//去重
		int count = groupMapper.checkGroupName(group.getGroupName(), -1);
		if(count > 0){
			return new Msg(false,"分组名重复!");
		}
		groupMapper.saveGroup(group);
		return new Msg(true,"操作成功!");
	}

	public Msg deleteGroup(int id) {
		//检查分组下是否有人员
		User user = new User();
		user.setGroupId(id);
		int count = userMapper.queryFilterUserCount(user);
		if(count > 0){
			return new Msg(false,"分组下存在用户,不能删除...");
		}
		//删除权限
		authMapper.deleteGroupAuth(id);
		//删除分组
		groupMapper.deleteGroup(id);
		return new Msg(true,"操作成功!");
	}

	public Msg modifyGroup(GroupInfo group) {
		//去重
		int count = groupMapper.checkGroupName(group.getGroupName(), group.getId());
		if(count > 0){
			return new Msg(false,"分组名重复!");
		}
		groupMapper.modifyGroup(group);
		return new Msg(true,"操作成功!");
	}

	public Msg queryAllGroup() {
		return new Msg(true,"", groupMapper.queryAllGroup());
	}

}
